﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;


namespace ChMetody_3
{
    public partial class Form1 : Form
    {

        ZedGraphControl ZedGraph;
        GraphPane pane;

        public void draw_func()
        {           
            pane.CurveList.Clear();
            PointPairList list = new PointPairList();
            double xmin = -100;
            double xmax = 100;
            double xmin_limit = -5;
            double xmax_limit = 5;
            double ymin_limit = -5;
            double ymax_limit = 10;
            for (double x = xmin; x <= xmax; x +=0.1)
            {
                list.Add(x, Functions.f("1*"+textBox4.Text, x));
            }
            LineItem myCurve = pane.AddCurve("Sinc", list, Color.Blue, SymbolType.None);
            pane.XAxis.Scale.Min = xmin_limit;
            pane.XAxis.Scale.Max = xmax_limit;
            pane.YAxis.Scale.Min = ymin_limit;
            pane.YAxis.Scale.Max = ymax_limit;
            ZedGraph.AxisChange();
            ZedGraph.Invalidate();
        }

        public Form1()
        {
            InitializeComponent();
            ZedGraph = zedGraphControl1;
            pane = ZedGraph.GraphPane;
            comboBox1.Visible = false;
            textBox1.Text = 0.00000001.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label2.Text = "x = ";
            textBox2.Text = "";
            textBox3.Text = "";
            pane.CurveList.Clear();
            ZedGraph.Invalidate();
            comboBox1.Items.Clear();
            comboBox1.Visible = false;
            radioButton1.Checked =
            radioButton2.Checked =
            radioButton3.Checked =
            radioButton4.Checked = false;            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "x = ";
            double a = Convert.ToDouble(textBox2.Text);
            double b = Convert.ToDouble(textBox3.Text);
            double eps = Convert.ToDouble(textBox1.Text);
            double x = 0;
            if (radioButton3.Checked)
            {
                comboBox1.Visible = true;
                List<double> korni = Heuristic.Solve(textBox4.Text, a, b, eps);
                foreach (double var in korni)
                    comboBox1.Items.Add(var.ToString("F6"));
            }
            else
            {
                if (a < b && Functions.f(textBox4.Text, a) * Functions.f(textBox4.Text, b) < 0)
                {
                    if (radioButton1.Checked)
                    {
                        x = HalfDivision.Method(textBox4.Text, a, b, eps);
                    }
                    if (radioButton2.Checked)
                    {
                        string[] equations = { textBox4.Text, textBox5.Text, textBox6.Text };
                        x = Newton.Solve(equations, a, b, eps);
                    }
                    if (radioButton4.Checked)
                    {
                        comboBox1.Visible = true;
                        List<double> korni = Functions.Ribakoff(a, b, textBox4.Text, eps);
                        foreach (double var in korni)
                        {
                            comboBox1.Items.Add(Math.Round(var, 6));
                        }
                    }

                    label2.Text = label2.Text + x.ToString("F6");
                }
                else
                    MessageBox.Show("Неправильный интервал");
            }               
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox5.Visible =
                textBox6.Visible =
                    label7.Visible =
                        label8.Visible = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            textBox5.Visible =
                textBox6.Visible =
                    label7.Visible =
                        label8.Visible = false;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            textBox5.Visible =
                textBox6.Visible =
                    label7.Visible =
                        label8.Visible = false;
        }

        private void zedGraphControl1_MouseClick(object sender, MouseEventArgs e)
        {
            double x, y;
            zedGraphControl1.GraphPane.ReverseTransform(e.Location, out x, out y);
            label11.Text = "X:" + Math.Round(x * 100000) / 100000;
            label12.Text = "Y:" + Math.Round(y * 100000) / 100000;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            draw_func();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
